package com.team7.happycommunity.businessservice.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team7.happycommunity.businessservice.common.CommonResult;
import com.team7.happycommunity.businessservice.entity.OrderClassify;
import com.team7.happycommunity.businessservice.entity.OrderDetails;
import com.team7.happycommunity.businessservice.entity.ServiceOrder;
import com.team7.happycommunity.businessservice.service.ServiceOrderService;
import io.swagger.annotations.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ServiceOrder)表控制层
 *
 * @author makejava
 * @since 2020-03-28 10:41:54
 */
@RestController
@RequestMapping("serviceOrder")
@Api(tags="服务订单接口")
public class ServiceOrderController {
    /**
     * 服务对象
     */
    @Resource
    private ServiceOrderService serviceOrderService;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ServiceOrder selectOne(Integer id) {
        return this.serviceOrderService.queryById(id);
    }

    /**
     * 根据商家id和交易状态查询商家的订单
     * @return
     */
    @GetMapping("/queryOrderByUserIdAndStatus")
    @ApiOperation(value = "查询商家的订单")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "status",value = "订单状态",required = true),
            @ApiImplicitParam(name = "limit",value = "条数",required = false),
            @ApiImplicitParam(name = "page",value = "页数",required = false)
    })
    public PageInfo queryOrderByUserIdAndStatus(Integer status,Integer limit,Integer page){
        System.out.println("statusstatusstatusstatus"+status);
        String userId = request.getHeader("userId");

        if (limit==null){
            limit=2;
        }
        if(page==null){
            page=1;
        }
        PageHelper.startPage(page,limit);
        List<OrderClassify> list = serviceOrderService.queryOrderByUserIdAndStatus(Integer.valueOf(userId), status);
        PageInfo pageInfo=new PageInfo(list);


        return pageInfo;
    }

    /**
     * 根据订单的id和订单状态修改订单状态
     * @param status 订单状态
     * @param id 订单的id
     * @return
     */
    @GetMapping("/alertStatus")
    @ApiOperation(value = "根据订单的id和订单状态修改订单状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "status",value = "修改后的订单状态",required = true),
            @ApiImplicitParam(name = "id",value = "订单的id",required = true)
    })

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "修改成功"),
            @ApiResponse(code = 500,message = "修改失败")
    })
    public CommonResult alertStatus(Integer status,Integer id){
        //订单退款修改状态流程
       if (status==1){
           try {
               Map map=new HashMap();
               map.put("id",id);
               map.put("status",status);
               //完成相应退款

               rabbitTemplate.convertAndSend("refund_code_exchange","refund."+id, map);
               System.out.println("退款完成");
           } catch (Exception e) {
               e.printStackTrace();
               return CommonResult.failed();
           }

           return CommonResult.success();
       }
       //其他流程
        try {
            serviceOrderService.updateById(id,status);
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 根据订单id查询订单详情，本次查询分三次sql语句查询
     * @param orderId
     * @return
     */
   @GetMapping("/queryOrder")
   @ApiOperation(value = "查询订单详情")
   @ApiImplicitParams(value = {
           @ApiImplicitParam(name = "orderId",value = "订单id",required = true)
   })
    public OrderDetails queryOrder(Integer orderId){
       return serviceOrderService.queryOrder(orderId);
    }

    /**
     * 根据订单id查询订单详情，本次查询分三次sql语句查询(此次查询服务的图片)
     * @param orderId 订单号id
     * @return
     */
    @GetMapping("/queryImage")
    @ApiOperation(value = "查询服务的图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderId",value = "订单id",required = true)
    })
    public OrderDetails queryImage(Integer orderId){
        List<OrderDetails>  orderDetails=serviceOrderService.queryImage(orderId);
        if(orderDetails.isEmpty()){
            return null;
        }
        return orderDetails.get(0);
    }

    /**
     * 根据订单id查询订单详情，本次查询分三次sql语句查询(此次查询服务的评论)
     * @param orderId 定点杆号id
     * @return
     */
    @GetMapping("/evaluate")
    @ApiOperation(value = "查询服务的评论")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderId",value = "订单id",required = true)
    })
    public List<OrderDetails> evaluate(Integer orderId){

        return  serviceOrderService.evaluate(orderId);
    }


}